#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 150;

int n, m;
int w[N][N];
int dp[N][N];
int path[N];

int main()
{
    cin >> n >> m; // 分公司数n，设备台数m；
    
    for (int i = 1; i <= n; i ++ )
       for (int j = 1; j <= m; j ++ )
            cin >> w[i][j]; // 体积为i 的价值为j

    
    for (int i = 1; i <= n; i ++ )
        for (int j = 0; j <= m; j ++ )
        {
            dp[i][j] = dp[i - 1][j];
            for (int k = 0; k <= j; k ++ )
                dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + w[i][k]);
        }
    cout << dp[n][m] << endl;
    
    
    // for (int i = 0; i <= n; i ++ )
    // {
    //     for (int j = 0; j <= m; j ++ )
    //         cout << dp[i][j] << " ";
    //     cout << endl;
    // }
    
    int j = m;
    for (int i = n; i; i -- )
        for (int k = 0; k <= j; k ++ )
            if (dp[i][j] == dp[i - 1][j - k] + w[i][k])
            {
                path[i] = k;
                j -= k;
                break;
            }
    
    for (int i = 1; i <= n; i ++ ) cout << i << " " << path[i] << endl;
    return 0;
}
